package com.qf.openapi2204.zuul;

import com.qf.openapi2204.common.constants.RedisConstants;
import com.qf.openapi2204.common.vo.ResultVo;
import com.qf.openapi2204.zuul.feignclient.CacheClient;
import com.qf.openapi2204.zuul.localcache.LocalCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@SpringBootApplication
@EnableZuulProxy  // 开启zuul网关的功能
@EnableFeignClients
public class ZuulApp {


    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(ZuulApp.class, args);

        // 加载缓存信息
        CacheClient cacheClient = context.getBean(CacheClient.class);

        // 获取需要加载的所有redis中的key
        ResultVo resultVo = cacheClient.keys(RedisConstants.APINAME_PREFIX + "*");
        List<String> keyList = (List<String>) resultVo.getData();

        for (String key : keyList) {
            HashMap<String,Object> routingInfo = (HashMap<String, Object>) cacheClient.hgetAll(key).getData();
            // 放入本地缓存中
            LocalCache.routingCache.put(key,routingInfo);
        }

    }
}
